<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Getting started on Windows</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>

<body bgcolor="#ffffff">
<H1><a name="Windows">3 Getting started on Windows </a></H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Windows_installation">Installation on Windows</a>
<ul>
<li><a href="#Windows_executable">Windows Executable</a>
</ul>
<li><a href="#Windows_examples">SWIG Windows Examples</a>
<ul>
<li><a href="#Windows_visual_studio">Instructions for using the Examples with Visual Studio</a>
<ul>
<li><a href="#Windows_csharp">C#</a>
<li><a href="#Windows_java">Java</a>
<li><a href="#Windows_python">Python</a>
<li><a href="#Windows_tcl">TCL</a>
</ul>
<li><a href="#Windows_other_compilers">Instructions for using the Examples with other compilers</a>
</ul>
<li><a href="#Windows_swig_exe">Building swig.exe on Windows</a>
<ul>
<li><a href="#Windows_cmake">Building swig.exe using CMake</a>
<li><a href="#Windows_msys2">Building swig.exe using MSYS2 and MinGW-w64</a>
<li><a href="#Windows_mingw_msys">Building swig.exe using MinGW and MSYS</a>
<li><a href="#Windows_cygwin">Building swig.exe using Cygwin</a>
<ul>
<li><a href="#Windows_examples_cygwin">Running the examples on Windows using Cygwin</a>
</ul>
</ul>
<li><a href="#Windows_interface_file">Microsoft extensions and other Windows quirks</a>
<ul>
<li><a href="#Windows_msvc_cpp_standards">Visual C++ standards compliance</a>
<li><a href="#Windows_calling_conventions">Calling conventions</a>
</ul>
</ul>
</div>
<!-- INDEX -->



<p>
This chapter describes SWIG usage on Microsoft Windows. 
Installing SWIG and running the examples is covered as well as building the SWIG executable.
Usage within the Unix like environments MinGW and Cygwin is also detailed.
</p>


<H2><a name="Windows_installation">3.1 Installation on Windows</a></H2>


<p>
SWIG does not come with the usual Windows type installation program, however it is quite easy to get started. The main steps are:
</p>
<ul>
        <li>Download the swigwin zip package from the <a href="https://www.swig.org">SWIG website</a> and unzip into a directory. This is all that needs downloading for the Windows platform.
        <li>Set environment variables as described in the <a href="#Windows_examples">SWIG Windows Examples</a> section in order to run examples using Visual C++.
</ul>

<H3><a name="Windows_executable">3.1.1 Windows Executable</a></H3>


<p>
The swigwin distribution contains the SWIG Windows 64-bit executable, swig.exe, which will only run on 64-bit versions of Windows.
If you want to build your own swig.exe have a look at <a href="#Windows_swig_exe">Building swig.exe on Windows</a>.
</p>


<H2><a name="Windows_examples">3.2 SWIG Windows Examples</a></H2>


<p>
Microsoft Visual C++ is used for compiling and linking SWIG's output on Windows for some languages.
However, MinGW and gcc is often the only supported toolchain for a number of target languages.
The Examples directory has a few Visual C++ project files (.vcxproj files) where the target languages are known to work with Visual C++.
These were produced by Visual Studio 2019.
Newer versions of Visual Studio, such as Visual Studio 2022, are able to open and convert these project files if necessary.
Each C# example comes with a Visual Studio 2019 solution in addition to the .vcxproj file the C# (.csproj) project file.
The project files have been set up to execute SWIG in a custom build rule for the SWIG interface (.i) file. 
Alternatively run the <a href="#Windows_examples_cygwin">examples using Cygwin</a>.

<p>
More information on each of the examples is available with the examples distributed with SWIG (Examples/index.html).

<H3><a name="Windows_visual_studio">3.2.1 Instructions for using the Examples with Visual Studio</a></H3>


<p>
Ensure the SWIG executable is as supplied in the SWIG root directory in order for the examples to work. 
Most languages require some environment variables to be set <b>before</b> running Visual C++. 
Note that Visual Studio must be re-started to pick up any changes in environment variables. 
Open up an example .vcxproj file or .sln file, Visual Studio will prompt you to upgrade the project if necessary.
Ensure the Release build is selected then do a Rebuild Solution from the Build menu.
The required environment variables are displayed with their current values during the build.
</p>
<p>
The list of required environment variables for each module language is also listed below.
They are usually set from the Control Panel and System properties, but this depends on which flavour of Windows you are running.
If you don't want to use environment variables then change all occurrences of the environment variables in the .dsp files with hard coded values.
If you are interested in how the project files are set up there is explanatory information in some of the language module's documentation.
</p>

<H4><a name="Windows_csharp">3.2.1.1 C#</a></H4>


<p>
The C# examples do not require any environment variables to be set as a C# project file is included.
Just open up the .sln solution file in Visual Studio 2019 or later, select Release Build, and do a Rebuild Solution from the Build menu.
The accompanying C# and C++ project files are automatically used by the solution file.
</p>


<H4><a name="Windows_java">3.2.1.2 Java</a></H4>


<p>
<b><tt>JAVA_INCLUDE</tt></b> : Set this to the directory containing jni.h<br>
<b><tt>JAVA_BIN</tt></b> : Set this to the bin directory containing javac.exe<p>
Example using the openjdk package installed in a Conda environment:<br>
<tt>
JAVA_INCLUDE: C:\miniconda3\envs\java\Library\lib\jvm\include<br>
JAVA_BIN: C:\miniconda3\envs\java\Library\lib\jvm\bin<br>
</tt>
</p>


<H4><a name="Windows_python">3.2.1.3 Python</a></H4>


<p>
<b><tt>PYTHON_INCLUDE</tt></b> : Set this to the directory that contains Python.h<br>
<b><tt>PYTHON_LIB</tt></b> : Set this to the Python library including path for linking<p>
Example using Python 3.13 installed in a Conda environment:<br>
<tt>
PYTHON_INCLUDE: C:\miniconda3\envs\python\include<br>
PYTHON_LIB: C:\miniconda3\envs\python\libs\python313.lib<br>
</tt>
</p>


<H4><a name="Windows_tcl">3.2.1.4 TCL</a></H4>


<p>
<b><tt>TCL_INCLUDE</tt></b> : Set this to the directory containing tcl.h<br>
<b><tt>TCL_LIB</tt></b> : Set this to the TCL library including path for linking<p>
Example using ActiveTcl 8.6<br>
<tt>
TCL_INCLUDE: C:\ActiveTcl\include<br>
TCL_LIB: C:\ActiveTcl\lib\tcl86t.lib<br>
</tt>
</p>


<H3><a name="Windows_other_compilers">3.2.2 Instructions for using the Examples with other compilers</a></H3>


<p>
If you do not have access to Visual C++ you will have to set up project files / Makefiles for your chosen compiler. There is a section in each of the language modules detailing what needs setting up using Visual C++ which may be of some guidance. Alternatively you may want to use Cygwin as described in the following section.
</p>

<H2><a name="Windows_swig_exe">3.3 Building swig.exe on Windows</a></H2>


<p>
The SWIG distribution provides a pre-built swig.exe and so it is not necessary for users to build the SWIG executable.
However, this section is provided for those that want to modify the SWIG source code in a Windows environment. 
Normally this is not needed, so most people will want to ignore this section.
</p>

<p>
There are various ways to build the SWIG executable including <a href="https://cmake.org/">CMake</a> which is able to generate project files for building with <a href="https://visualstudio.microsoft.com/">Visual Studio</a> <a href="https://docs.microsoft.com/cpp/">MSVC</a>.<br>
SWIG can also be compiled and run using <a href="https://www.msys2.org/">MSYS2</a> with <a href="https://www.mingw-w64.org/">MinGW-w64</a>, <a href="https://www.cygwin.com">Cygwin</a> or <a href="https://osdn.net/projects/mingw/">MinGW</a>, all of which provide a Unix like front end to Windows and comes free with the <a href="https://gcc.gnu.org/">GCC</a> C/C++ compiler.<br>
SWIG can also be compiled with MSYS2 using MSVC and SWIG <a href="https://github.com/swig/cccl">MSVC wrapper</a>.<br>
</p>


<H3><a name="Windows_cmake">3.3.1 Building swig.exe using CMake</a></H3>


<p>
SWIG can be built using <a href="https://cmake.org/">CMake</a> and Visual Studio rather than autotools. As with the other approaches to 
building SWIG the dependencies need to be installed. The steps below are one of a number of ways of installing the dependencies without requiring Cygwin or MinGW.
For fully working build steps always check the Continuous Integration (CI) setups currently detailed in the <a href="https://github.com/swig/swig/tree/master/.github/workflows/nuget.yml">GitHub Actions YAML file</a>.
</p>

<ol>
    <li>
        Install Nuget from <a href="https://www.nuget.org/downloads">https://www.nuget.org/downloads</a> (v6.0.0 is used in this example, and installed to <tt>C:\Tools</tt>). Nuget is the package manager
        for .NET, but allows us to easily install <a href="https://cmake.org/">CMake</a> and other dependencies required by SWIG.
    </li>
    <li>
        Install <a href="https://www.nuget.org/packages/CMake-win64/">CMake-win64 Nuget package</a> using the following command: <pre>C:\Tools\nuget install CMake-win64 -Version 3.15.5 -OutputDirectory C:\Tools\CMake</pre>
        Using PowerShell the equivalent syntax is: <pre>&amp; "C:\Tools\nuget" install CMake-win64 -Version 3.15.5 -OutputDirectory C:\Tools\CMake</pre>
        Alternatively you can download CMake from <a href="https://cmake.org/download/">https://cmake.org/download/</a>.
    </li>
    <li>
        Install the <a href="https://www.nuget.org/packages/bison/">Bison Nuget package</a> using the following command: <pre>C:\Tools\nuget install Bison -Version 3.7.4 -OutputDirectory C:\Tools\bison</pre>
        Alternatively download Bison from <a href="https://sourceforge.net/projects/winflexbison/files/">https://sourceforge.net/projects/winflexbison/files/</a> (Bison 3.7.4 is used in this example)
        and save to a folder e.g. <tt>C:\Tools\Bison</tt>
    </li>
    <li>
        Install the <a href="https://www.nuget.org/packages/pcre2/">PCRE2 Nuget package</a> using the following command: <pre>C:\Tools\nuget install PCRE2 -Version 10.39 -OutputDirectory C:\Tools\pcre2</pre>
        Note this is a x64 build, if this is not suitable PCRE2 can be built from source using <a href="https://github.com/PhilipHazel/pcre2/">https://github.com/PhilipHazel/pcre2/</a>.
        Alternatively, set <tt>WITH_PCRE=OFF</tt> to disable PCRE2 support if you are sure you do not require it. 
    </li>
    <li>
        We will also need the SWIG source code. Either download a zipped archive from GitHub, or if git is installed clone the latest codebase
        using: <pre>git clone https://github.com/swig/swig.git</pre>
        In this example we are assuming the source code is available at <tt>C:\swig</tt>
    </li>
    <li>
    <p>
        Now we have all the required dependencies we can build SWIG using PowerShell and the commands below. We are assuming Visual Studio 2019 is installed. For other versions of Visual Studio change <tt>"Visual Studio 16 2019 -A x64"</tt> to the relevant
        <a href="https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators">Visual Studio Generator</a> and
        architecture. We add the required build tools to the system PATH, and then
        build a Release version of SWIG. If all runs successfully a new swig.exe should be generated in the <tt>C:/swig/install2/bin</tt> folder.
    </p>
    </li>
</ol>
<div class="shell"><pre>
cd C:\swig

$env:PATH="C:\Tools\CMake\CMake-win64.3.15.5\bin;C:\Tools\bison\Bison.3.7.4\bin;" + $env:PATH
$PCRE_ROOT="C:\Tools\pcre2\PCRE2.10.39.0"

cmake -G "Visual Studio 16 2019" -A "x64" `
-DCMAKE_INSTALL_PREFIX="C:/swig/install2" `
-DCMAKE_C_FLAGS="/DPCRE2_STATIC" `
-DCMAKE_CXX_FLAGS="/DPCRE2_STATIC" `
-DPCRE2_INCLUDE_DIR="$PCRE_ROOT/include" `
-DPCRE2_LIBRARY="$PCRE_ROOT/lib/pcre2-8-static.lib" `
-S . -B build

cmake --build build --config Release
cmake --install build --config Release

# to test the exe built correctly
cd install2/bin
./swig.exe -version
./swig.exe -help
</pre></div>

<p>
    In addition to Release builds you can create a Debug build using:
</p>
<div class="shell">
    <pre>cmake --build build --config Debug</pre>
</div>
<p>
    A Visual Studio solution file should be generated named swig.sln. This can be opened and debugged by running the swig project and setting <tt>Properties &gt; Debugging &gt; Command Arguments</tt>. For example to debug one of the test-suite .i files included with the SWIG source use the following:
</p>
<div class="shell">
    <pre>-python -c++ -o C:\Temp\doxygen_parsing.cpp C:\swig\Examples\test-suite\doxygen_parsing.i</pre>
</div>

<H3><a name="Windows_msys2">3.3.2 Building swig.exe using MSYS2 and MinGW-w64</a></H3>


<p>
Download and install MSYS2 from <a href="https://www.msys2.org/">www.msys2.org</a> (tested with version msys2-x86_64-20201109).
Launch the MSYS2 shell.
</p>
<p>
Install the packages needed to build swig:<br>
</p>

<div class="shell">
<pre>
pacman -S git autoconf automake bison gcc make pcre2-devel
</pre>
</div>

<p>
Clone the repository to /usr/src/:
</p>

<div class="shell">
<pre>
mkdir /usr/src/
cd /usr/src/
git clone https://github.com/swig/swig.git
</pre>
</div>

<p>
Configure and build:
</p>

<div class="shell">
<pre>
cd /usr/src/swig
./autogen.sh
./configure
make
</pre>
</div>

<p>
Finally you may also want to install SWIG:
</p>

<div class="shell">
<pre>
make install
</pre>
</div>

<H3><a name="Windows_mingw_msys">3.3.3 Building swig.exe using MinGW and MSYS</a></H3>


<p>Warning: These instructions were added in 2006 and have barely changed since
so are unlikely to work exactly as written.</p>

<p>
The short abbreviated instructions follow...
</p>

<ul>
    <li>Install MinGW and MSYS from the <a href="https://osdn.net/projects/mingw/">MinGW</a> site. This provides a Unix environment on Windows.
    <li>Follow the usual Unix instructions in the README file in the SWIG root directory to build swig.exe from the MinGW command prompt.
</ul>

<p>
The step by step instructions to download and install MinGW and MSYS, then download and build the latest version of SWIG from Github follow...
Note that the instructions for obtaining SWIG from Github are also online at <a href="https://www.swig.org/svn.html">SWIG Bleeding Edge</a>.
</p>

<p>
<b>Pitfall note:</b>
Execute the steps in the order shown and don't use spaces in path names. In fact it is best to use the default installation directories.
</p>

<ol>
  <li>
  Download the following packages from the <a href="https://osdn.net/projects/mingw/releases/">MinGW download page</a>.
  Note that at the time of writing, the majority of these are in the Current
  release list and some are in the Snapshot or Previous release list.
  <ul>
    <li>MinGW-3.1.0-1.exe</li>
    <li>MSYS-1.0.11-2004.04.30-1.exe</li>
    <li>msysDTK-1.0.1.exe</li>
    <li>bison-2.0-MSYS.tar.gz</li>
    <li>msys-autoconf-2.59.tar.bz2</li>
    <li>msys-automake-1.8.2.tar.bz2</li>
  </ul>
  </li>

  <li>
  Install MinGW-3.1.0-1.exe (C:\MinGW is default location.)
  </li>

  <li>
  Install MSYS-1.0.11-2004.04.30-1.exe. Make sure you install it on the same
  windows drive letter as MinGW (C:\msys\1.0 is default).
  In the post install script,
  <ul>
    <li>Answer y to the "do you wish to continue with the post install?"</li>
    <li>Answer y to the "do you have MinGW installed?"</li>
    <li>Type in the folder in which you installed MinGW (C:/MinGW is default)</li>
  </ul>
  </li>

  <li>
  Install msysDTK-1.0.1.exe to the same folder that you installed MSYS (C:\msys\1.0 is default).
  </li>

  <li>
  Copy the following to the MSYS install folder (C:\msys\1.0 is default):
  <ul>
    <li>msys-automake-1.8.2.tar.bz2</li>
    <li>msys-autoconf-2.59.tar.bz2</li>
    <li>bison-2.0-MSYS.tar.gz</li>
  </ul>
  </li>

  <li>
  Start the MSYS command prompt and execute:
<div class="shell"><pre>
cd /
tar -jxf msys-automake-1.8.2.tar.bz2
tar -jxf msys-autoconf-2.59.tar.bz2
tar -zxf bison-2.0-MSYS.tar.gz
</pre></div>
  </li>

  <li>
  The very latest development version of SWIG is available from <a href="https://github.com/swig/swig">SWIG on Github</a>
  and can be downloaded as a zip file or if you have Git installed, via Git.
  Either download the latest <a href="https://github.com/swig/swig/archive/master.zip">Zip file</a> snapshot and unzip and rename the top level folder to /usr/src/swig.

  Otherwise if using Git, type in the following:
<div class="shell"><pre>
mkdir /usr/src
cd /usr/src
git clone https://github.com/swig/swig.git
</pre></div>

  <b>Pitfall note:</b>
If you want to place SWIG in a different folder to the proposed
/usr/src/swig, do not use MSYS emulated windows drive letters, because
the autotools will fail miserably on those.
  </li>

  <li>
The PCRE2 third party library needs to be built next.
Download the latest PCRE2 source tarball, such as <tt>pcre2-10.39.tar.bz2</tt>, from
<a href=https://www.pcre.org>www.pcre.org</a> and place in the <tt>/usr/src/swig</tt> directory.
Build PCRE2 as a static library using the Tools/pcre-build.sh script as follows:

<div class="shell"><pre>
cd /usr/src/swig
Tools/pcre-build.sh
</pre></div>
  </li>

  <li>
  You are now ready to build SWIG. Execute the following commands to build swig.exe:
<div class="shell"><pre>
cd /usr/src/swig
./autogen.sh
./configure
make
</pre></div>
  </li>
</ol>


<H3><a name="Windows_cygwin">3.3.4 Building swig.exe using Cygwin</a></H3>


<p>
Note that SWIG can also be built using Cygwin.
However, SWIG will then require the Cygwin DLL when executing. 
Follow the Unix instructions in the README file in the SWIG root directory.
Note that the Cygwin environment will also allow one to regenerate the autotool generated files which are supplied with the release distribution. 
These files are generated using the <tt>autogen.sh</tt> script and will only need regenerating in circumstances such as changing the build system.
</p>


<H4><a name="Windows_examples_cygwin">3.3.4.1 Running the examples on Windows using Cygwin</a></H4>


<p>
The examples and test-suite work as successfully on Cygwin as on any other Unix operating system. 
The modules which are known to work are Python, Tcl, Perl, Ruby, Java and C#.
Follow the Unix instructions in the README file in the SWIG root directory to build the examples.
</p>

<H2><a name="Windows_interface_file">3.4 Microsoft extensions and other Windows quirks</a></H2>


<H3><a name="Windows_msvc_cpp_standards">3.4.1 Visual C++ standards compliance</a></H3>


<p>
The Visual C++ compiler (MSVC) has a long history of not being standards compliant, but this has been getting better
over the years.
</p>

<p>
SWIG's <a href="SWIGPlus.html#SWIGPlus_nn3">approach for C++</a> is to generate standards compliant C++98 code along with
enhancements for later standards if the C++ compiler supports a later standard.
Unfortunately by default, in 2024, Visual C++ still does not set the <tt>__cplusplus</tt> macro correctly to pick up these later C++ standard features.
MSVC users are urged to ensure this macro is defined correctly by consulting the latest Microsoft Visual C++ documentation, in particular,
<a href="https://learn.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version">/std</a> and 
<a href="https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus">/Zc:__cplusplus</a>.
</p>

<H3><a name="Windows_calling_conventions">3.4.2 Calling conventions</a></H3>


<p>
A common problem when using SWIG on Windows are the Microsoft function calling conventions which are not in the C++ standard.
SWIG parses ISO C/C++ so cannot deal with proprietary conventions such as <tt>__declspec(dllimport)</tt>, <tt>__stdcall</tt> etc.
There is a Windows interface file, <tt>windows.i</tt>, to deal with these calling conventions though.
The file also contains typemaps for handling commonly used Windows specific types such as <tt>__int64</tt>, <tt>BOOL</tt>, <tt>DWORD</tt> etc.
Include it like you would any other interface file, for example:
</p>

<div class="code"><pre>
%include &lt;windows.i&gt;

__declspec(dllexport) ULONG __stdcall foo(DWORD, __int32);
</pre></div>

<p>Note that if you follow Microsoft's recommendation of wrapping the
<tt>__declspec</tt> calls in a preprocessor definition, you will need to
make sure that the definition is included by SWIG as well, by either defining it
manually or via a header. For example, if you have specified the
preprocessor definition in a header named <tt>export_lib.h</tt> and include
other headers which depend on it, you should use the <tt>%include</tt> directive
to include the definition explicitly. For example, if you had a header file,
<tt>bar.h</tt>, which depended on <tt>export_lib.h</tt>, your SWIG definition
file might look like:</p>

<div class="code"><pre>
// bar.i
%module bar
%include &lt;windows.i&gt;
%include "export_lib.h"
%include "bar.h"
</pre></div>

<p>
where export_lib.h may contain:
</p>

<div class="code"><pre>
// export_lib.h
#define BAR_API __declspec(dllexport)
</pre></div>

<p>
and bar.h may look like:
</p>

<div class="code"><pre>
// bar.h
#include "export_lib.h"
BAR_API void bar_function(int, double);
</pre></div>

<p>
Using the preprocessor to remove BAR_API is a popular simpler solution:
</p>

<div class="code"><pre>
// bar.i
%module bar
#define BAR_API
%include "bar.h"
</pre></div>

</body>
</html>
